package com.uchengwang.ui.cm_ui.umsg.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.ui.base_ui.ActivityTitleBar;
import com.uchengwang.ui.base_ui.ui.AllBaseActivity;
import com.uchengwang.ui.cm_ui.umsg.http.ChatHttpManager;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.UCommonUtil;

/**
 * Created by U城网 on 2017/12/15.
 */

public class EditorWantChatInfoActivity extends AllBaseActivity implements View.OnClickListener{
    //编辑不同的信息对应不同的type
    //1编辑享聊的昵称  2编辑享聊的宣言
    private int type;
    private ActivityTitleBar titleBa;
    private EditText editor;
    private TextView num, editor_dec;
    private int length;
    private String editString, typeS = "";
    private Button editor_bt;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editor_want_chat);

        typeS = getIntent().getStringExtra(BaseConfig.CONTENT);//上个界面带过来的数据
        type = getIntent().getIntExtra(BaseConfig.TYPE, -1);
        initView();

    }
    private void initView(){
        titleBa = (ActivityTitleBar) findViewById(R.id.want_editor_titleBa);
        editor = (EditText) findViewById(R.id.want_editor_editor);
        editor.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (!TextUtils.isEmpty(charSequence)){
                    num.setText(charSequence.length()+"/"+length);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
        num = (TextView) findViewById(R.id.want_editor_num);
        editor_dec = (TextView) findViewById(R.id.want_editor_dec);
        editor_bt = (Button) findViewById(R.id.want_editor_bt);
        editor_bt.setOnClickListener(this);
        editor_bt.setText("确定修改");



        initViewData();

    }

    private void initViewData(){

        switch (type){
            case 1:
                titleBa.setTitle("享聊昵称", 0);
                editor.setFilters(new InputFilter[]{new InputFilter.LengthFilter(15)});
                num.setText("0/15");
                length = 15;
                break;
            case 2:
                titleBa.setTitle("享聊宣言", 0);
                editor.setFilters(new InputFilter[]{new InputFilter.LengthFilter(100)});
                num.setText("0/100");
                length = 100;
                editor_dec.setText("系统会自动把你的享聊宣言发送给你享聊的Ta哦");
                break;

        }

        if (!TextUtils.isEmpty(typeS)){
            editor.setText(typeS);
            editor.setSelection(typeS.length());
            num.setText(typeS.length()+"/"+length);
        }

//        titleBa.setRightText("帮助", new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//
//            }
//        });
    }

    /**
     * 将信息提交到
     * */
    private void postInfo(MapRequest map){
        ChatHttpManager.getManager().changeWantInfo(this, map, new OnResultListener() {
            @Override
            public void onResult(int stutas, String msg, int total, String data) {
                if (stutas ==0){
                    UCommonUtil.getUtil().showToast(EditorWantChatInfoActivity.this, "修改成功");

                    Intent intent = new Intent();
                    intent.putExtra(BaseConfig.CONTENT, editString);
                    intent.putExtra(BaseConfig.TYPE, type);
                    setResult(1, intent);
                    EditorWantChatInfoActivity.this.finish();
                }else {
                    UCommonUtil.getUtil().showToast(EditorWantChatInfoActivity.this, msg);
                }
            }
        });

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.want_editor_bt:
                //完成提交网络
                editString = editor.getText().toString();
                editString = editString.trim();

                if (!TextUtils.isEmpty(typeS) && typeS.equals(editString)){
                    EditorWantChatInfoActivity.this.finish();
                    return;
                }

                LoadingUtils.getUtils().loading(EditorWantChatInfoActivity.this);
                MapRequest map = new MapRequest();
                map.put(BaseConfig.CHAT_TYPE, 1);
                switch (type){
                    case 1://享聊昵称
                        map.put(BaseConfig.CHAT_NAME, editString);
                        break;
                    case 2://享聊宣言
                        map.put(BaseConfig.CHAT_SIGN, editString);
                        break;
                }
                postInfo(map);

                break;
        }
    }
}
